<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('合作基金投资项目汇总') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('在管基金')" prop="fundId">
        <MainBodySelect v-model="queryParams.fundId" :show-label="false" parameters="fund" :scope-authority="true" />
      </el-form-item>
      <el-form-item :label="$t('周期')" prop="yearMonth">
        <el-date-picker
          v-model="queryParams.yearMonth"
          :clearable="false"
          type="month"
          value-format="yyyy-MM"
          :placeholder="$t('周期')"
        />
      </el-form-item>
      <el-form-item :label="$t('仅查看吴江区项目')" prop="isWuJiang">
        <dict-select
          v-model="queryParams.isWuJiang"
          dictionary="sys_yes_no"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dealBfInvestmentDealInfoList" :span-method="objectSpanMethod" :show-summary="true" :summary-method="getSummaries" tooltip-effect="darkTable" border>
      <el-table-column :label="$t('序号')" type="index" align="center" width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金名称')" align="center" prop="dealName" show-overflow-tooltip min-width="200">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id: scope.row.dealId, companyId: scope.row.companyId, gpId: scope.row.gpId, dealType: 'bf'}}"
            class="link_a"
          >{{ scope.row.companyName }}</router-link>
        </template>
      </el-table-column>

      <el-table-column :label="$t('基金属性/类型')" align="center" prop="fundAttributesLabel" show-overflow-tooltip min-width="200" />
      <el-table-column :label="$t('投资项目名称')" align="center" prop="bfInvestDealName" show-overflow-tooltip min-width="200" />
      <el-table-column :label="$t('项目行业类别')" align="center" prop="industryName" show-overflow-tooltip min-width="200" />
      <el-table-column :label="$t('企业注册资本') + $t('金额单位')" align="center" prop="capitalRegistered" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.capitalRegistered, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目注册地址')" align="center" prop="detailsRegistered" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('项目本轮估值') + $t('金额单位')" align="center" prop="companyWholeVal" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.companyWholeVal, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('投资日期')" align="center" prop="contributionTime" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('基金认缴金额') + $t('金额单位')" align="center" prop="agreementAmount" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.agreementAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金实际投资额') + $t('金额单位')" align="center" prop="transferAmount" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.transferAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('所持股权比例') + $t('(%)')" align="center" prop="latestShareholdingRatio" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.latestShareholdingRatio, undefined, 2, true) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('委派董监高情况')" align="center" prop="delegationSituation" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('项目主营业务')" align="center" prop="businessScope" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('项目投资阶段')" align="center" prop="enterpriseStage" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('基金对企业投资部分的最新估值') + $t('金额单位')" align="center" prop="valuationResult" show-overflow-tooltip min-width="230">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.valuationResult, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金对企业投资部分的最新估值') + $t('金额单位')" align="center" prop="masterFundHoldValuationResult" show-overflow-tooltip min-width="240">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.masterFundHoldValuationResult, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目退出情况')" align="center" prop="exitStatus" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('退出时间')" align="center" prop="moneyBackDate" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('退出方式')" align="center" prop="exitPattern" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('收回资金') + $t('金额单位')" align="center" prop="quitTotalAmount" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.quitTotalAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('其中：本金') + $t('金额单位')" align="center" prop="quitCapital" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.quitCapital, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('其中：基金分红及收益') + $t('金额单位')" align="center" prop="quitProfitAndShareBonus" show-overflow-tooltip min-width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.quitProfitAndShareBonus, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否江苏省企业')" align="center" prop="isJiangSu" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('是否苏州市企业')" align="center" prop="isSuZhou" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('实际经营地')" align="center" prop="actualBusinessLocation" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('是否上市')" align="center" prop="isListed" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('股票代码')" align="center" prop="stockCode" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('是否新三板企业')" align="center" prop="isNewThirdBoardEnterprises" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('新三板代码')" align="center" prop="newThirdBoardCode" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('是否属于中小企业')" align="center" prop="isMinorEnterprises" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('是否独角兽企业')" align="center" prop="isUnicornEnterprise" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('是否瞪羚企业')" align="center" prop="isGazelleEnterprise" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('是否创新型企业')" align="center" prop="isInnovativeEnterprises" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('备注')" align="center" prop="remark" show-overflow-tooltip min-width="150" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import MainBodySelect from '@/components/MainBodySelect'
import { getBfInvestmentDealInfoList, getBfInvestmentDealInfoSum } from '@/api/sass/dfgz/dfgzReport'
export default {
  name: 'BfInvestmentDealInfoList',
  components: {
    MainBodySelect
  },
  data() {
    return {
      dealBfInvestmentDealInfoList: [],
      dealBfInvestmentDealInfoSum: {},
      total: 0,
      loading: false,
      queryParams: {
        fundId: undefined,
        yearMonth: this.getNowFormatDate(),
        isWuJiang: 'N',
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getBfInvestmentDealInfoList(this.queryParams).then(response => {
        getBfInvestmentDealInfoSum(this.queryParams).then(res => {
          this.dealBfInvestmentDealInfoSum = res.data
        })
        this.dealBfInvestmentDealInfoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    // 获取当前日期函数
    getNowFormatDate() {
      const date = new Date()
      const year = date.getFullYear() // 获取完整的年份(4位)
      let month = date.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
      // let strDate = date.getDate() // 获取当前日(1-31)
      // if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
      return `${year}-${month}`
    },
    exportHandle() {
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        this.downLoadExcel('/dfgz/report/getBfInvestmentDealInfoList/export', this.queryParams)
      }).catch(() => {
      })
    },
    objectSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex <= 1) {
        const spanArr = this.rowspan()
        for (let i = 0; i < spanArr.length; i++) {
          const _row = spanArr[rowIndex]
          return {
            rowspan: _row,
            colspan: 1
          }
        }
      }
    },
    rowspan() {
      // 二维数组，用于存放单元格合并规则
      const spanArr = []
      // 用于存储相同项的开始index
      let position = 0
      this.dealBfInvestmentDealInfoList.forEach((item, index) => {
        if (index === 0) {
          spanArr.push(1)
          position = 0
        } else {
          if (this.dealBfInvestmentDealInfoList[index].dealId === this.dealBfInvestmentDealInfoList[index - 1].dealId) {
            spanArr[position] += 1 // 有相同项
            spanArr.push(0) // 名称相同后往数组里面加一项0
          } else {
            spanArr.push(1) // 同列的前后两行单元格不相同
            position = index
          }
        }
      })
      return spanArr
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 5) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.capitalRegistered, 'w')
        } else if (index === 7) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.companyWholeVal, 'w')
        } else if (index === 9) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.agreementAmount, 'w')
        } else if (index === 10) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.transferAmount, 'w')
        } else if (index === 15) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.valuationResult, 'w')
        } else if (index === 16) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.masterFundHoldValuationResult, 'w')
        } else if (index === 20) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.quitTotalAmount, 'w')
        } else if (index === 21) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.quitCapital, 'w')
        } else if (index === 22) {
          sums[index] = this.moneyFormat(this.dealBfInvestmentDealInfoSum.quitProfitAndShareBonus, 'w')
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
